package com.cxdr.awsips.packing.processor;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.cxdr.awsips.packing.bean.Target;
import com.cxdr.awsips.packing.config.LinkConfig;
import com.cxdr.awsips.packing.config.SystemConfig;
import com.cxdr.awsips.packing.file.FileHelper;
import com.cxdr.awsips.packing.file.FileHelperFactoy;
import com.cxdr.awsips.packing.logger.LogFactory;

public class FileScaner {
	private static Logger logger = LogFactory.makeNewLogger(FileScaner.class);
	// 扫描到的文件数
	private static int scanedCnt = 0;
	private Timer timer;
	private boolean isStop = true;
	//private List<Target> sources = LinkConfig.getSources();
	public FileScaner() {
		timer = new Timer();
	}

	public void start() {
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				String sourcePath = SystemConfig.get("path.source");
					try {
						logger.info("开始扫描目录"+sourcePath+"....");
						File sourcePathDir = new File(sourcePath);
						if(!sourcePathDir.isDirectory()){
							logger.info("目录"+sourcePath+"不存在，请检查源目录");
//							FileScaner.this.cancel();
							return;
						}
						//扫描目录
						File[] files = sourcePathDir.listFiles();
						logger.info("扫描到 "+files.length+"个文件" );
						for(File file:files){
							//过滤包文件和更新文件
							if(file.getName().contains("BENN")){
								continue;
							}
							if(file.getName().contains("CC")){
								//备份文件
								String backpath = SystemConfig.get("path.pack");
								if(backpath!=null){
									file.renameTo(new File(backpath+File.separator+file.getName()));
								}
							}
							FileChecker.FileQueue.add(file.getName());
							scanedCnt++;
						}
					} catch (Exception e) {
						logger.error("获取源"+sourcePath+"错误",e);
						e.printStackTrace();
					}
			}
		}, 1000,30000);
		isStop = false;
	}
	/**
	 * 扫描本地
	 * @param source
	 */
	private void scanLocal(Target source){ 
		String sourcePath = source.getPath();
		if(StringUtils.isBlank(sourcePath)){
			sourcePath = SystemConfig.get("path.source");
		}
		logger.info("开始扫描目录" + sourcePath + "....");
		File sourcePathDir = new File(sourcePath);
		if (!sourcePathDir.isDirectory()) {
			logger.info("目录" + sourcePath + "不存在，请检查源目录");
			// FileScaner.this.cancel();
			return;
		}
		// 扫描目录
		File[] files = sourcePathDir.listFiles();
		logger.info("扫描到 " + files.length + "个文件");
		for (File file : files) {
			String fileName = file.getName();
			if(StringUtils.endsWithIgnoreCase(fileName, ".txt")){
				FileChecker.FileQueue.add(file.getName());
				scanedCnt++;
			}
		}
	}
	
	/**
	 * 扫描FTP，并把FTP文件下载带源目录
	 * @param source
	 */
	private void scanFtp(Target source){ 
		
	}
	public void cancel() {
		timer.cancel();
		isStop = true;
	}

	public boolean isStop() {
		return isStop;
	}

	public void setStop(boolean isStop) {
		this.isStop = isStop;
	}

	public static int getScanedCnt() {
		return scanedCnt;
	}
}
